package syspizza.telas;

// <editor-fold defaultstate="collapsed" desc="imports">   
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import syspizza.Pizza;
import syspizza.cliente.Cliente;
import syspizza.massa.Massa;
import syspizza.massa.factory.MassaFactory;
import syspizza.DAO.PedidoDAO;
import syspizza.pedido.Pedido;
import syspizza.pedido.item.IItem;
import syspizza.DAO.SaborDAO;
import syspizza.sabor.Sabor;
import syspizza.tableModel.TableModelGenerico;
import syspizza.tipos.OPERACAO;
// </editor-fold> 

public class DialogPedido extends javax.swing.JDialog {

    private Pedido pedido;
    private OPERACAO operacao;

    public DialogPedido(java.awt.Frame parent, boolean modal, OPERACAO operacao, Pedido pedido) {
        super(parent, modal);
        initComponents();

        this.operacao = operacao;
        switch (operacao) {
            case ALTERACAO:
                this.pedido = pedido;
                trataInicioEdicao();
                break;
            case INCLUSAO:
                this.pedido = new Pedido(new Cliente(), new ArrayList<IItem>(), new Date(), 0);
                break;
            default:
                break;
        }

        setLocationRelativeTo(null);
        setResizable(false);
        carregaMassas();
        carregaSabores();
        this.btRemoverItem.setEnabled(false);

    }

    private void trataInicioEdicao() {
        this.tfClientePedido.setText(this.pedido.getCliente().getNome());
        carregarInfos();
        atualizaTabelaItens();
        carregaTotal();
    }

    private void carregarInfos() {
        //endereço tele cel obser
        StringBuilder sb = new StringBuilder();
        sb.append("ENDEREÇO:  ")
                .append(this.pedido.getCliente().getEndereco())
                .append("\nCOMPLEMENTO:  ").append(this.pedido.getCliente().getComplemento())
                .append("   CEP:  ").append(this.pedido.getCliente().getCEP())
                .append("\nTELEFONE:  ").append(this.pedido.getCliente().getTelefone())
                .append("\nCELULAR:  ").append(this.pedido.getCliente().getCelular())
                .append("\nOBSERVAÇÃO:  ").append(this.pedido.getCliente().getObservacao());
        this.taDadosCliente.setText(sb.toString());
    }

    private void atualizaTabelaItens() {
        TableModelGenerico modelo = new TableModelGenerico(pedido.getItens());
        this.tablePizzas.setModel(modelo);
    }

    private boolean podeSalvar() {
        return ((this.pedido.getCliente() != null) && (!this.pedido.getItens().isEmpty()));
    }

    private void salvarPedido() {
        if (!podeSalvar()) {
            JOptionPane.showMessageDialog(this, "Para salvar o pedido é necessário de um cliente informado e pizzas adicionadas",
                    "Inclusão",
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        boolean ok = false;
        switch (operacao) {
            case ALTERACAO:
                ok = new PedidoDAO().update(pedido);
                break;
            case INCLUSAO:
                ok = new PedidoDAO().insert(pedido);
                break;
            default:
                return;
        }
        if (ok) {
            JOptionPane.showMessageDialog(this, "Pedido salvo com sucesso!", "Inclusão", JOptionPane.INFORMATION_MESSAGE);
            dispose();
        } else {
            JOptionPane.showMessageDialog(this, "Não foi possível salvar registro!", "Inclusão", JOptionPane.ERROR_MESSAGE);
        }
    }

    private Massa getMassa() {
        String chave = (String) this.cbMassas.getSelectedItem();
        Massa massa = (Massa) MassaFactory.getInstance().getMassa(chave);
        return massa;
    }

    private void chamaTelaPesquisaCliente() {
        DialogCliente dialog = new DialogCliente(null, true);
        dialog.setVisible(true);
        Cliente c = dialog.getCliente();

        if (c != null) {
            this.pedido.setCliente(c);
            this.tfClientePedido.setText(this.pedido.getCliente().getNome());
            this.carregarInfos();
        }
    }

    private void carregaTotal() {
        NumberFormat format = new DecimalFormat("###,##0.00");
                            //String s = format.format((float) ob);

        if (!this.pedido.getItens().isEmpty()) {
            //usando lambda, isso nao é de Deus
            float total = (float) this.pedido.getItens()
                    .parallelStream()
                    .mapToDouble(p -> p.getValorVenda())
                    .sum();
            this.ftfValorTotal.setText(format.format(total));
        } else {
            this.ftfValorTotal.setText(format.format(0f));
        }
    }

    private void carregaSabores() {
        List<Sabor> sabores = new SaborDAO().getAll();

        for (Sabor sabor : sabores) {
            this.cbSabores.addItem(sabor);
        }
    }

    private void carregaMassas() {
        List<String> chaves = (List<String>) MassaFactory.getInstance().getFactories();
        for (String s : chaves) {
            this.cbMassas.addItem(s);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        btBuscarClientePedido = new javax.swing.JButton();
        tfClientePedido = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablePizzas = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        taDadosCliente = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        cbSabores = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        cbMassas = new javax.swing.JComboBox();
        btAdicionarPizza = new javax.swing.JButton();
        btRemoverItem = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        ftfValorTotal = new javax.swing.JFormattedTextField();
        btSalvarPedido = new javax.swing.JButton();
        btFecharTelaPedido = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.ABOVE_TOP));

        jLabel3.setText("Cliente:");

        btBuscarClientePedido.setText("Buscar");
        btBuscarClientePedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btBuscarClientePedidoActionPerformed(evt);
            }
        });

        tfClientePedido.setEditable(false);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tfClientePedido, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btBuscarClientePedido, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(btBuscarClientePedido, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfClientePedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        tablePizzas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Descrição", "Qtde", "Valor"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tablePizzas.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tablePizzas.getTableHeader().setReorderingAllowed(false);
        tablePizzas.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                tablePizzasFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                tablePizzasFocusLost(evt);
            }
        });
        jScrollPane1.setViewportView(tablePizzas);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Dados", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.ABOVE_TOP));

        taDadosCliente.setEditable(false);
        taDadosCliente.setColumns(20);
        taDadosCliente.setFont(new java.awt.Font("Monospaced", 1, 13)); // NOI18N
        taDadosCliente.setRows(5);
        jScrollPane2.setViewportView(taDadosCliente);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Pizza", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.ABOVE_TOP));

        jLabel5.setText("Sabor:");

        jLabel4.setText("Massa:");

        btAdicionarPizza.setText("Adicionar");
        btAdicionarPizza.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAdicionarPizzaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbSabores, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(10, 10, 10)
                        .addComponent(cbMassas, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btAdicionarPizza, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbSabores)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbMassas)
                            .addComponent(jLabel4)))
                    .addComponent(btAdicionarPizza, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE))
                .addContainerGap())
        );

        btRemoverItem.setText("Remover");
        btRemoverItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRemoverItemActionPerformed(evt);
            }
        });

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.ABOVE_TOP));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Total R$:");

        ftfValorTotal.setEditable(false);
        ftfValorTotal.setForeground(new java.awt.Color(255, 0, 0));
        ftfValorTotal.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("###,##0.00"))));
        ftfValorTotal.setAlignmentX(10.0F);
        ftfValorTotal.setCaretColor(new java.awt.Color(240, 240, 240));
        ftfValorTotal.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ftfValorTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(ftfValorTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(btRemoverItem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btRemoverItem, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        btSalvarPedido.setText("Salvar");
        btSalvarPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarPedidoActionPerformed(evt);
            }
        });

        btFecharTelaPedido.setText("Fechar");
        btFecharTelaPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btFecharTelaPedidoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(311, 311, 311)
                        .addComponent(btSalvarPedido, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btFecharTelaPedido, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btSalvarPedido, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                    .addComponent(btFecharTelaPedido, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btBuscarClientePedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btBuscarClientePedidoActionPerformed
        chamaTelaPesquisaCliente();
    }//GEN-LAST:event_btBuscarClientePedidoActionPerformed

    private void btAdicionarPizzaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAdicionarPizzaActionPerformed

        Sabor sabor = (Sabor) this.cbSabores.getSelectedItem();
        if (sabor == null) {
            JOptionPane.showMessageDialog(this, "É necessário ter um sabor para a Pizza", "Sabor não encontrado", JOptionPane.WARNING_MESSAGE);
            return;
        }

        Massa massa = getMassa();
        IItem pizza = new Pizza(sabor, massa);
        pedido.getItens().add(pizza);
        atualizaTabelaItens();
        carregaTotal();
    }//GEN-LAST:event_btAdicionarPizzaActionPerformed

    private void btSalvarPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarPedidoActionPerformed

        salvarPedido();
    }//GEN-LAST:event_btSalvarPedidoActionPerformed

    private void btRemoverItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRemoverItemActionPerformed

        int linha = this.tablePizzas.getSelectedRow();
        if (linha == -1) {
            return;
        }
        this.pedido.getItens().remove(linha);
        atualizaTabelaItens();
        carregaTotal();

    }//GEN-LAST:event_btRemoverItemActionPerformed

    private void tablePizzasFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tablePizzasFocusGained
        int linha = this.tablePizzas.getSelectedRow();
        this.btRemoverItem.setEnabled(linha > -1);
    }//GEN-LAST:event_tablePizzasFocusGained

    private void tablePizzasFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tablePizzasFocusLost

    }//GEN-LAST:event_tablePizzasFocusLost

    private void btFecharTelaPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btFecharTelaPedidoActionPerformed
        dispose();
    }//GEN-LAST:event_btFecharTelaPedidoActionPerformed

// <editor-fold defaultstate="collapsed" desc="campos gerados">  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAdicionarPizza;
    private javax.swing.JButton btBuscarClientePedido;
    private javax.swing.JButton btFecharTelaPedido;
    private javax.swing.JButton btRemoverItem;
    private javax.swing.JButton btSalvarPedido;
    private javax.swing.JComboBox cbMassas;
    private javax.swing.JComboBox cbSabores;
    private javax.swing.JFormattedTextField ftfValorTotal;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea taDadosCliente;
    private javax.swing.JTable tablePizzas;
    private javax.swing.JTextField tfClientePedido;
    // End of variables declaration//GEN-END:variables

// </editor-fold> 
}
